﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cocon90.Lib.Dal.Store
{
    /// <summary>
    /// 可查询的Store，继承自EditableStore
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SearchStore<T> : EditableStore<T> where T : class,Rule.IModel, new()
    {
        public SearchStore()
        {
            OldSearchSQLString = this.OldSelectSQLString;
        }
        /// <summary>
        /// 获取 默认查询的源SQL语句
        /// </summary>
        public string OldSearchSQLString { get; private set; }
        /// <summary>
        /// 按自定义Where子句（不需要写where单词）查询数据库
        /// </summary>
        /// <param name="customWhere"></param>
        public void Search(string customWhere)
        {
            this.SelectSQLString = OldSearchSQLString;
            this.SelectSQLString = String.Format("select * from ({0}) searchStoreTempTable{1} where {2}", this.OldSelectSQLString, DateTime.Now.Millisecond, customWhere);
            this.ForceLoadStaticDataOneTime();
            this.Load(false);
        }
        /// <summary>
        /// 以列为键，以模糊值为值 的词典方式进行查询
        /// </summary>
        /// <param name="ColumnsLikeValue"></param>
        public void Search(Dictionary<string, object> ColumnsLikeValue)
        {
            this.SelectSQLString = OldSearchSQLString;
            this.SelectSQLString = String.Format("select * from ({0}) searchStoreTempTable{1} where {2}", this.OldSelectSQLString, DateTime.Now.Millisecond, Tools.sqlHelper.getWhereLikeSql(ColumnsLikeValue));
            this.ForceLoadStaticDataOneTime();
            this.Load(false);
        }
        /// <summary>
        /// 以模型实体来模糊查询
        /// </summary>
        /// <param name="model"></param>
        public void Search(T model)
        {
            Dictionary<string, object> ColumnsLikeValue = new Dictionary<string, object>();
            foreach (var prop in typeof(T).GetProperties())
            {
                var val = prop.GetValue(model, null);
                ColumnsLikeValue.Add(prop.Name, val);
            }
            Search(ColumnsLikeValue);
        }
        /// <summary>
        /// 过滤当前加载出来的数据。指定按哪一个属性来过滤，以及它的值，第三参不是是否是模糊查询（默认为true）
        /// </summary>
        public void Filter(string propertie, object value, bool isFogging = true)
        {
            this.FilterFunction += (index, propertie_out, value_out) =>
            {
                if (isFogging)
                {
                    //只显示选中列中包含过滤条件的。
                    return propertie_out == propertie && (value_out + "").Contains(value + "");
                }
                else return propertie_out == propertie && value_out + "" == value;
            };
            this.Load(true);//传入True表示启用过滤的加载。
        }
        /// <summary>
        /// 过滤当前显示的记录中，位置索引在[startIndex,endIndex]范围内（闭区间）的。
        /// </summary>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        public void Filter(int startIndex, int endIndex)
        {
            var mh = new Tools.ModelHelper<T>();
            var primeryKey = mh.GetPrimeryKey();
            this.FilterFunction += (index, propertie_out, value_out) =>
            {
                //只显示每页中的前10条记录
                var type = typeof(T);

                return (propertie_out == primeryKey) && (index % this.PageSize) <= endIndex && (index % this.PageSize) >= startIndex;
            };
            this.Load(true);//传入True表示启用过滤的加载。
        }
    }
}
